package com.yixun.lbx.service.impl;

import com.yixun.entity.DiQu;
import com.yixun.entity.YxUser;
import com.yixun.entity.YxUserRole;
import com.yixun.lbx.entity.YxProCompany;
import com.yixun.lbx.mapper.company.CompanyMapper;
import com.yixun.lbx.service.CompanyService;
import com.yixun.lbx.service.LbxRedisService;
import com.yixun.mapper.UserMapper;
import com.yixun.mapper.UserRoleMapper;
import com.yixun.util.FormatUtil;
import com.yixun.util.MD5Util;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class CompanyServiceImpl implements CompanyService {
    private final CompanyMapper companyMapper;
    private final UserMapper userMapper;
    private final LbxRedisService redisService;
    private final UserRoleMapper userRoleMapper;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public CompanyServiceImpl(CompanyMapper companyMapper, UserMapper userMapper, LbxRedisService redisService, UserRoleMapper userRoleMapper) {
        this.companyMapper = companyMapper;
        this.userMapper = userMapper;
        this.redisService = redisService;
        this.userRoleMapper = userRoleMapper;
    }

    @Override
    public List<YxProCompany> getListByPage(HashMap<String, String> map) {
        return companyMapper.getCompanyListByPage(map);
    }

    @Override
    public Integer CompanyCount() {
        return companyMapper.getCount();
    }

    @Override
    public YxProCompany getCompanyByIdOrNameOrCode(YxProCompany company) {
        return companyMapper.getCompanyByIdOrNameOrCode(company);
    }

    @Override
    public int addCompany(YxProCompany company) {
        /*
         * 生成企业代码 1位企业类型代码+Qy+5位流水号 第一位1表示生产企业 2表示加工企业 9表示其他
         *用户账号=企业代码enterpriseCode
         *密码Qy@+5位流水号
         *删除管理对象时的时候，对应的用户账号表的状态从正常变为停用，是否删除=是
         */
        String code = redisService.getKey("enterpriseCode");
        if (code.equals("null") || code.equals("")) {
            String maxCode = companyMapper.getMaxCode();
            if (maxCode == null || maxCode.equals("")) {
                maxCode = "10000";
            }
            redisService.setKeyIncrValue("enterpriseCode", Integer.valueOf(maxCode));
        }
        Long enterpriseCode = redisService.incr("enterpriseCode");
        company.setCode("Qy" + enterpriseCode);
        company.setIsDelete(0);
        company.setAddTime(dateFormat.format(new Date()));
        YxUser yxUser = new YxUser();
        yxUser.setUser_name(company.getCode());
        yxUser.setUser_realname(company.getLegalMan());
        yxUser.setUser_phone(company.getPhone());
        yxUser.setDept_id("1");
        yxUser.setUser_type(2);
        yxUser.setDq_id(Long.valueOf(company.getDq4Id()));
        yxUser.setAdd_user(company.getAddUser());
        yxUser.setUser_id(new FormatUtil().getUUID());
        yxUser.setAdd_time(dateFormat.format(new Date()));
        yxUser.setPassword(new MD5Util().MD5("Qy@" + "12345"));
        userMapper.insertUser(yxUser);
        /*
         * 给 企业账号设置角色 ，固定 Role_id = e054bc9392d54a5390a96df94f399278
         * 系统角色发生变化时  这里可能会失效 ，需要管理员手动为企业账号添加 关联角色
         * */
        try {
            YxUserRole userRole = new YxUserRole();
            userRole.setRole_id("e054bc9392d54a5390a96df94f399278");
            userRole.setUser_id(yxUser.getUser_id());
            userRole.setUser_role_id(new FormatUtil().getUUID());
            userRoleMapper.insertUserRole(userRole);
        } catch (Exception e) {
            e.printStackTrace();
        }
        int i = companyMapper.insetCompay(company);
        return i;
    }

    @Override
    public int updateCompany(YxProCompany company) {
        company.setUpdateTime(dateFormat.format(new Date()));
        return companyMapper.updateCompany(company);
    }

    @Override
    public int deleteCompany(HashMap<String, Object> map) {
        String updateTime = dateFormat.format(new Date());
        map.put("updateTime", updateTime);
        String[] qyIds = map.get("qyId").toString().split(",");
        map.put("qyIds", qyIds);
        /*设置企业用户停用*/
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("user_status", "1");
        List<String> id = companyMapper.getCodeById(map);
        if (id.size() != 0) {
            map1.put("user_id", id);
            companyMapper.setUserStatus(map1);
        }
        return companyMapper.deleteCompany(map);
    }

    @Override
    public HashMap<String, Object> getIsDeleteListByPage(HashMap<String, String> map) {
        List<YxProCompany> companies = companyMapper.getIsDeleteListByPage(map);
        Integer count = companyMapper.getIsDeleteCount(map);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("companies", companies);
        hashMap.put("count", count);
        return hashMap;
    }

    @Override
    public Integer enableCompany(HashMap<String, Object> map) {
        map.put("updateTime", dateFormat.format(new Date()));
        String[] qyIds = map.get("qyId").toString().split(",");
        map.put("qyIds", qyIds);
        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("user_status", "0");
        List<String> id = companyMapper.getCodeById(map);
        if (id.size()>0) {
            map1.put("user_id", id);
            companyMapper.setUserStatus(map1);
        }
        return companyMapper.enableCompany(map);
    }

    @Override
    public List<YxProCompany> getSearchListByPage(HashMap<String, String> map) {
        return companyMapper.getSearchListByPage(map);
    }
    
    @Override
    public List<DiQu> getChildDiquListByDq3Name(String area) {
        return companyMapper.getChildDiquListByDq3Name(area);
        
    }
    
    
}
